#include "CountMe.h"

CountMe::CountMe(QWidget *parent)
    : QWidget(parent)
{
s = QDesktopWidget().screenGeometry();
setFixedSize(s.width(),s.height());
setFont(QFont("Arial",4));
QVBoxLayout * mainLayout = new QVBoxLayout;
mainLayout->setSpacing(2);
marq = new Marquee();
cnt = new Counter(QString("settings.dat"),50,s.width()-20);
list = new Mylist(s.height()-150, s.width()-20);
newone = new Newfile(this, NULL, s.height(), s.width());
conf = new MyConfig("config.dat", s.height(),s.width());
hlp = new MyHelp(s.height(),s.width());
marq->fromFile(list->current());
cnt->loadCnf(list->current());

help = new QLabel("*-config<br>#-help");
mainLayout->addWidget(marq);
mainLayout->addWidget(cnt);
//mainLayout->addWidget(list);
//mainLayout->addWidget(help);
//QHBoxLayout * lst = new QHBoxLayout;
//lst->addWidget(list);
//lst->addWidget(help);
//mainLayout->addLayout(lst);
mainLayout->addWidget(new QLabel("Dua list:"));
mainLayout->addWidget(list);
mainLayout->addWidget(help);
mainLayout->addStretch();
setLayout(mainLayout);

QObject::connect(list,SIGNAL(currentIndexChanged(const QString &)),marq,SLOT(fromFile(const QString &)));
QObject::connect(list,SIGNAL(currentIndexChanged(const QString &)),cnt,SLOT(loadCnf(const QString &)));
QObject::connect(newone,SIGNAL(savedSth()),list,SLOT(reloadList()));
setFocus();
}
void CountMe::keyPressEvent(QKeyEvent *event){
//marq->setText(QString("%1 %2").arg(event->key()).arg(event->text()));
int k = event->key();
if (k == conf->conf["add"]) 
	cnt->add(conf->conf["addby"]);
else if (k == conf->conf["reset"]){
	cnt->reset();
} else if (k == conf->conf["save"]){
	cnt->save();
} else if (k == conf->conf["next"]){
	list->next();
} else if (k == conf->conf["prev"]){
	list->prev();
} else if (k == conf->conf["edit"]){
	newone->fromFile(list->current());
	newone->show();
} else if (k == conf->conf["del"]){
	newone->deleteEntry(list->current());
} else if (k == conf->conf["new"]){
	newone->reset();
	newone->show();
} else if (k == conf->conf["start"]){
	marq->start();
} else if (k == conf->conf["stop"]){
	marq->stop();
} else if (k == 42) conf->show();
else if (k == 35) {
	hlp->show();
	hlp->setFocus();
}
else QWidget::keyPressEvent(event);
}

CountMe::~CountMe()
{
cnt->save();
}
